package com.zhou.test.spring.secrity.model.security;

import com.google.common.collect.Lists;
import com.zhou.test.spring.secrity.entity.SysUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xianfeng
 * @date 2023/5/13 12:06
 */
@Data
public class MyUserDetails implements UserDetails {

    private SysUser sysUser;

    private String userId;

    private String username;

    private String phone;

    private String currOrgId;

    private String currOrgName;

    public MyUserDetails(SysUser sysUser) {
        this.sysUser = sysUser;
        this.userId = sysUser.getUserId() + "";
        this.username = sysUser.getUserName();
        this.phone = sysUser.getPhonenumber();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<String> authorities = Lists.newArrayList("ROLE_ADMIN", "system");
        return authorities.stream().map(t -> new SimpleGrantedAuthority(t)).collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return !sysUser.getStatus();
    }
}
